﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.SystemProtocolConfig;

namespace BCData.TMS.SystemProtocolConfig
{
    public class SystemProtocolConfigData : ISystemProtocolConfigData
    {
        private readonly IDatabaseContext databaseContext;
        private readonly string QUERY = @"SELECT
		    SystemProtocolConfigKey,
		    ProtocolTitle,
		    ProtocolContent,
		    CreateAdminId,
		    CreateAdminName,
		    CreateTime,
		    UpdateTime,
		    EditAdminId,
		    EditAdminName
        FROM
            systemprotocolconfig";



        #region 构造函数
        public SystemProtocolConfigData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #endregion

        #region InsertAsync
        public async Task<SystemProtocolConfigEntity> InsertAsync(SystemProtocolConfigEntity entity)
        {
            return await this.databaseContext.Master.InsertAsync(entity);
        }
        #endregion

        #region updateAsync
        public async Task<SystemProtocolConfigEntity> UpdateAsync(SystemProtocolConfigEntity entity)
        {
            return await this.databaseContext.Master.UpdateAsync(entity);
        }
        #endregion 

        #region Delete
        public async Task<bool> DeleteAsync(string systemProtocolConfigKey)
        {
            string sql = "DELETE FROM systemprotocolconfig WHERE SystemProtocolConfigKey = ?systemProtocolConfigKey";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SystemProtocolConfigKey", systemProtocolConfigKey, DbType.AnsiString);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region GetById
        public async Task<SystemProtocolConfigEntity> GetEntityAsync(string systemProtocolConfigKey)
        {
            string sql = QUERY + " WHERE  SystemProtocolConfigKey = ?systemProtocolConfigKey";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SystemProtocolConfigKey", systemProtocolConfigKey, DbType.AnsiString);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<SystemProtocolConfigEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询系统协议配置记录
        public async Task<(IEnumerable<SystemProtocolConfigEntity>, int)> GetListAsync(string protocolTitle,string protocolContent,int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if (!string.IsNullOrWhiteSpace(protocolTitle))
            {

                wheres.Add("ProtocolTitle  LIKE  ?ProtocolTitle");
                parameters.Add("?ProtocolTitle",$"%{protocolTitle.Trim()}%" , DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(protocolContent))
            {

                wheres.Add("ProtocolContent LIKE ?ProtocolContent");
                parameters.Add("?ProtocolContent", $"%{protocolContent.Trim()}%" , DbType.AnsiString);
            }

            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  systemprotocolconfig  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.databaseContext.QueryMultipleAsync<SystemProtocolConfigEntity, int>(sql, parameters);
        }
        #endregion

    }
}
//接口



